#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define endl '\n'
const int N = 2e5 + 5;

int n, m;
int a[N], num;
void discrete() {
  map<int, int> mp;
  rep(i, 1, n) {
    if (!mp.count(a[i])) mp[a[i]] = ++num;
    a[i] = mp[a[i]];
  }
}
int pos[N];
int l[505], r[505];
int mnl[N], mnr[N], mxl[N], mxr[N];
int mx, ans[N];
void init() {
  int k = sqrt(n);
  rep(i, 1, n) pos[i] = (i - 1) / k + 1;
  int cnt = pos[n];
  rep(i, 1, cnt) {
    l[i] = (i - 1) * k + 1;
    r[i] = i * k;
  }
  r[cnt] = n;
  //
  rep(i, 1, num) mnl[i] = mnr[i] = n + 1;
  rep(i, 1, num) mxl[i] = mxr[i] = 0;
  mx = 0;
}
struct Query {
  int l, r, id;
  bool operator<(Query x) const {
    if (pos[l] != pos[x.l]) return l < x.l;
    return r < x.r;
  }
} q[N];
int solve_inb(int L, int R) {
  int res = 0;
  rep(i, L, R) {
    mnl[a[i]] = min(mnl[a[i]], i), mxl[a[i]] = max(mxl[a[i]], i);
    res = max(res, mxl[a[i]] - mnl[a[i]]);
  }
  rep(i, L, R) mnl[a[i]] = n + 1, mxl[a[i]] = 0;
  return res;
}
void add_r(int i) {
  mnr[a[i]] = min(mnr[a[i]], i), mxr[a[i]] = max(mxr[a[i]], i);
  mx = max(mx, mxr[a[i]] - mnr[a[i]]);
}
void add_l(int i) {
  mnl[a[i]] = min(mnl[a[i]], i), mxl[a[i]] = max(mxl[a[i]], i);
  mx = max(mx, max(mxr[a[i]], mxl[a[i]]) - mnl[a[i]]);
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> n;
  rep(i, 1, n) cin >> a[i];
  discrete();
  init();
  cin >> m;
  rep(i, 1, m) {
    cin >> q[i].l >> q[i].r;
    if (q[i].l > q[i].r) swap(q[i].l, q[i].r);
    q[i].id = i;
  }
  sort(q + 1, q + m + 1);
  int j = 1;
  rep(i, 1, pos[n]) {
    int L = r[i] + 1, R = r[i];
    mx = 0;
    for (; pos[q[j].l] == i; j++) {
      if (pos[q[j].r] == i)
        ans[q[j].id] = solve_inb(q[j].l, q[j].r);
      else {
        // move to right
        while (R < q[j].r) add_r(++R);
        int last = mx;
        // move to left
        while (L > q[j].l) add_l(--L);
        ans[q[j].id] = mx;
        // cancel left
        while (L < r[i] + 1) {
          mnl[a[L]] = n + 1, mxl[a[L]] = 0;
          L++;
        }
        mx = last;
      }
    }
    // cancel right
    while (R > r[i]) {
      mnr[a[R]] = n + 1, mxr[a[R]] = 0;
      R--;
    }
  }
  rep(i, 1, m) cout << ans[i] << endl;
  return 0;
}